layui.define(function (exports) {

    var helper = {
        hasKey: function (obj, key) {
            return obj.hasOwnProperty(key)
        }
        , emptyKey: function (obj, key) {
            return !obj.hasOwnProperty(key) || (obj[key] === '' || obj[key] === null);
        }
        , hasKeyFnt: function (obj, key, callback) {
            if (obj.hasOwnProperty(key)) callback(obj, key)
        }
    };

    helper.getValue = function (obj, key, defaultValue) {
        if (this.hasKey(obj, key)) return obj[key];
        else return defaultValue;
    };

    helper.removeValue = function (obj, key, defaultValue) {
        if (this.hasKey(obj, key)) {
            var v = obj[key];
            delete obj[key];
            return v;
        } else {
            return defaultValue;
        }
    };

    helper.getPrimaries = function (condition, data) {
        var ids = [];
        if (typeof condition == 'function') {
            ids = condition(data);
        } else {
            ids = this.columns(data, condition);
        }
        return ids;
    };

    helper.getCondition = function (key, data) {
        var params = {};
        if (typeof key == 'function') {
            params = key(data);
        } else if (key) {
            params[key] = data[key];
        }
        return params;
    };

    helper.urlEncode = function (param) {
        if (param == null) return '';
        var arr = [];
        layui.each(param, function (k, v) {
            arr.push(k + '=' + v)
        });
        return arr.join('&');
    };

    helper.columns = function (data, key) {
        var d = [];
        layui.each(data, function (k, v) {
            d.push(v[key]);
        })
        return d;
    };

    var tableConfig = function () {
        this.headers = {};
        this.where = {conditions: {}, sort: {}};
        this.page = {};
    };

    tableConfig.prototype.setProperty = function (k, v) {
        this[k] = v;
    };

    tableConfig.prototype.setHeader = function (key, value) {
        this.headers[key] = value
    };

    tableConfig.prototype.setCondition = function (key, value) {
        this.where.conditions[key] = value
    };

    tableConfig.prototype.setConditions = function (conditions) {
        this.where.conditions = conditions
    };

    tableConfig.prototype.setSort = function (obj) {
        this.where.sort = obj
    };

    tableConfig.prototype.setSortValue = function (key, value) {
        this.where.sort[key] = value
    };

    tableConfig.prototype.clearSort = function () {
        this.where.sort = {}
    }

    tableConfig.prototype.setPage = function (key, value) {
        this.page[key] = value;
    };

    helper.getTableConfig = function () {
        return new tableConfig();
    };

    exports('helper', helper)

});